/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.controller;

import cn.webrx.mapper.CarouselMaper;
import cn.webrx.mapper.PermissionMapper;
import cn.webrx.mapper.UserLogMapper;
import cn.webrx.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Project: ssmapp - IndexController
 * <p>Powered by webrx On 2022-03-29 11:48:02
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@Controller
public class IndexController {
    @Autowired
    private PermissionMapper pm;

    @Autowired
    private CarouselMaper cm;

    @Autowired
    private UserLogMapper ulm;
    @RequestMapping
    public String index(Model m){
        m.addAttribute("msg","汉化版中心信息");
        return "index";
    }

    @GetMapping("/api/carouseldb") @ResponseBody
    public Map<String,Object> carouseldb(){
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",4);
        map.put("data",cm.queryAll());
        return map;
    }

    @RequestMapping("/carousel")
    public String carousel(Model m){
        m.addAttribute("banners",cm.queryAll());
        return "carousel";
    }


    @GetMapping("/init") @ResponseBody
    public Map<String,Object> init(HttpSession s){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> m1 = new HashMap<>();
        m1.put("title","首页");
        m1.put("href","home");
        map.put("homeInfo",m1);

        Map<String,String> m2 = new HashMap<>();
        m2.put("title","碳积分管理系统");
        m2.put("image","images/logo.png");
        m2.put("href","home");
        map.put("logoInfo",m2);

        Map<String,Object> menu = new HashMap<>();
        menu.put("title","默认左侧菜单");
        menu.put("icon","fa fa-address-book");
        menu.put("href","javascript:void(0)");
        menu.put("target","_self");
        menu.put("child",pm.menu(s.getAttribute("user").toString()));
        List<Map<String,Object>> menus = new ArrayList<>();
        menus.add(menu);
        map.put("menuInfo",menus);
        return map;
    }

    @RequestMapping("/home")
    public String home(Model m,HttpSession s){
        //m.addAttribute("logs",ulm.queryAll());
        m.addAttribute("banners",cm.show());
        m.addAttribute("logs",ulm.query(s.getAttribute("user").toString()));
        return "home";
    }

    @GetMapping("/login")
    public String login(){
        return "login";
    }
}
